package
{
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	import gl.gestures.GestureFlick;
	import gl.touches.TouchDown;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import XML;
	import flash.net.URLLoader;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.navigateToURL;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import com.greensock.*;
	import com.greensock.easing.*
	import id.core.Application;
	import id.core.TouchSprite;
	import gl.events.TouchEvent;
	import gl.events.GestureEvent
	import flash.utils.setTimeout;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.plugins.AutoAlphaPlugin;
	
	/**
	 * ...
	 * @author Aydarka
	 */
	public class Main extends Application
	{
		private var Cathegory_list:Array = new Array();
		private var imageMain_list:Array = new Array();
		private var imageMainTxt_list:Array = new Array();
		private var image_list:Array = new Array();
		private var bigImage_list:Array = new Array();
		private var title_list:Array = new Array();
		private var txt_list:Array = new Array();
		
		private var i:Number;
		private var tn:Number = 0;
		private var scale_factor:Number = 0.6;
		private var tween_duration:Number = 0.8;
		
		private var angle:Number;
		private var degree:Number
		private var total:Number;
		private var totalPic:Number;
		private var flashmo_xml:XML = new XML();
		private var folder:String = "thumbnails/";
		private var xml_loader:URLLoader = new URLLoader();
		private var thumbnail_group:TouchSprite;
		private var default_y:Number;
		private var flashmo_mc:TouchSprite;
		
		private var loaderName:String;
		private var bigpicLoader:Loader;
		private var loaderStats:TouchEvent;
		private var Xint:Number;
		private var FlickCheker:Number = 0;
		private var Cicl:Number = 0;
		private var NextEvent:GestureEvent;
		var tweenNext:TweenLite;
		var thumbsource:String;
		var thumb:Picture;
		var mainSprite:TouchSprite = new TouchSprite;
		var pileTouch:TouchSprite;
		var targetpile:TouchSprite;
		var targetPhoto:TouchSprite;
		var targetphotoX:Number;
		var targetphotoY:Number;
		var targetphotoR:Number;
		var targetInd:Number = 0;
		var pileMain:TouchSprite
		var photoTouch:TouchSprite;
		var targetPic:TouchSprite;
		var targetPic2:TouchSprite;
		var X:Array = new Array();
		var Y:Array = new Array();
		var mc_speedXTO: Array=new Array();
var mc_speedYTO: Array=new Array();
var mc_speedXFROM: Array=new Array();
var mc_speedYFROM: Array=new Array();
var speed:Number = 20;
		public function Main():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			addChild(mainSprite);
			xml_loader.load(new URLRequest("flashmo_129_thumbnail_list.xml"));
			xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);
			
			setTimeout(load, 1000);
			
			TweenPlugin.activate([AutoAlphaPlugin]);
			//	this.addEventListener(GestureEvent.GESTURE_FLICK_1, to_next);
			//	addEventListener(TouchEvent.TOUCH_MOVE, rotate);
		
		}
		
		private function create_thumbnail(e:Event):void
		{
			flashmo_xml = XML(e.target.data);
			total = flashmo_xml.cathegory.length();
			
			for (i = 0; i < total; i++)
			{
				var ph:XMLList = flashmo_xml.cathegory[i].pics
				image_list = []
				for (var k in ph)
				{
					
					thumbsource = folder + ph[k].@url;
					trace("УРЛ такой  " + (folder + ph[k].@url))
					thumb = new Picture(thumbsource, k);
					image_list.push(thumb);
					
				}
				imageMain_list.push(image_list);
				
			}
		
		}
		
		public function load():void
		{
			for (var j:int = 0; j < imageMain_list.length; j++)
			{
				pileMain = new TouchSprite;
				//pileMain.x = (300 * j)+100
				//pileMain.y=100
				pileMain.name = "pileMain" + j
				
				if (j < 3)
				{
					pileMain.x = (j * 300) + 100;
					pileMain.y = 100;
				}
				else
				{
					pileMain.y = mainSprite.getChildByName("pileMain" + (j - 3)).y + 200;
					pileMain.x = mainSprite.getChildByName("pileMain" + (j - 3)).x;
				}
				
				mainSprite.addChild(pileMain);
				pileMain.addEventListener(TouchEvent.TOUCH_TAP, touchChildre);
				for (var z:int = 0; z < imageMain_list[j].length; z++)
				{
					var arrayObj:TouchSprite = new TouchSprite
					arrayObj = imageMain_list[j][z];
					arrayObj.rotation = Math.random() * (70 * Math.random()) * 150;
					arrayObj.x = Math.random() * 20;
					arrayObj.y = Math.random() * 20;
					pileMain.addChild(arrayObj);
					
				}
				
			}
		}
		
		private function touchChildre(e:TouchEvent):void
		{
			
			for (var j:int = 0; j < mainSprite.numChildren; j++)
			{
				pileTouch = mainSprite.getChildByName("pileMain" + j) as TouchSprite;
				if (pileTouch.name !== e.currentTarget.name)
				{
					TweenLite.to(pileTouch, 1, {autoAlpha: 0, ease: Back.easeOut});
					
				}
			}
			targetpile = e.currentTarget as TouchSprite;
			trace(targetpile.name);
		
			targetpile.removeEventListener(TouchEvent.TOUCH_TAP, touchChildre);
			Y = []
			X = []
			addEventListener(Event.ENTER_FRAME,moveBalls);
			TweenLite.to(targetpile, 3, {x: 400, y: 384, ease: Back.easeOut, onComplete: photoPlace, onCompleteParams: [null,null,null]});
		
		}
		
		private function photoPlace(targetphotoX:Number, targetphotoY:Number, targetphotoR:Number):void
		{
			
			if (targetInd < targetpile.numChildren)
			{
				targetPhoto = targetpile.getChildAt(targetInd) as TouchSprite;
				targetPhoto.addEventListener(TouchEvent.TOUCH_TAP, touchHandler);
				TweenLite.to(targetPhoto, 0.1, {x: targetphotoX, y: targetphotoY, rotation: targetphotoR, ease: Elastic.easeOut, onComplete: photoPlaceAgain});
				targetInd++
			}
		}
		
		private function photoPlaceAgain():void
		{
			
			if (targetInd < targetpile.numChildren)
			{
				totalPic = targetpile.numChildren
				angle = Math.PI * 4 / totalPic
				degree = Math.round(angle * 180 / Math.PI);
				
				if (targetInd < totalPic / 2)
				{
					targetphotoX = (Math.cos(targetInd * angle) * 120) + (Math.random() * 50)
					targetphotoY = (Math.sin(targetInd * angle) * 120) + (Math.random() * 50)
					targetphotoR = (targetInd * degree)
					
				}
				else
				{
					targetphotoX = (Math.cos(targetInd * angle) * 200) + (Math.random() * 50)
					targetphotoY = (Math.sin(targetInd * angle) * 200) + (Math.random() * 50)
					
					targetphotoR = (targetInd * degree)
				}
				
			}
			X[targetInd]=targetphotoX
			Y[targetInd] = targetphotoY
			mc_speedXFROM[i]=0;
      mc_speedYFROM[i]=0;
			photoPlace(targetphotoX, targetphotoY, targetphotoR);
		}
		
		private function touchHandler(e:TouchEvent):void
		{
			
		//	targetPic = e.currentTarget as TouchSprite;
				//
	
		
		}
		
function moveBalls(e:Event):void {
	
    for(var i=0; i<targetpile.numChildren; i++){
        var mc:TouchSprite = targetpile.getChildAt(i) as TouchSprite
        var distance_posX:Number = X[i] - mc.x;
        var distance_posY:Number = Y[i] - mc.y;
        var dist_pos = Math.sqrt(distance_posX*distance_posX + distance_posY*distance_posY);
        var angle:Number = Math.atan2(distance_posY, distance_posX);
        mc_speedXTO[i] = Math.cos(angle) * speed;
        mc_speedYTO[i] = Math.sin(angle) * speed;
        mc.x += mc_speedXTO[i];
        mc.y += mc_speedYTO[i];
        if(dist_pos<mc.width/2){
            mc.x=X[i];
            mc.y=Y[i];
        }
        var distance_mouseX:Number = mouseX-targetpile.x - mc.x;
        var distance_mouseY:Number = mouseY-targetpile.y - mc.y;
        var dist_mouse = Math.sqrt(distance_mouseX*distance_mouseX + distance_mouseY*distance_mouseY);
        if(dist_mouse<60){
            var mc_angle=Math.atan2(distance_mouseY,distance_mouseX);
            mc_speedXFROM[i]=Math.cos(mc_angle)*speed*4;
            mc_speedYFROM[i]=Math.sin(mc_angle)*speed*4;
        }
            mc.x-=mc_speedXFROM[i];
            mc.y-=mc_speedYFROM[i];
            mc_speedXFROM[i]*=0.9;
            mc_speedYFROM[i]*=0.9;
    }
}
	
	}

}

